package com.minglv.mini.service.impl;

import com.minglv.mini.domain.Need;
import com.minglv.mini.domain.NeedMsg;
import com.minglv.mini.domain.User;
import com.minglv.mini.domain.dto.NMsgDto;
import com.minglv.mini.domain.dto.NeedDto;
import com.minglv.mini.domain.dto.NeedMsgVo;
import com.minglv.mini.mapper.NeedMapper;
import com.minglv.mini.mapper.NeedMsgMapper;
import com.minglv.mini.mapper.UserMapper;
import com.minglv.mini.service.INeedService;
import com.minglv.mini.tools.DateUtil;
import com.minglv.mini.tools.JWTUtils;
import com.minglv.mini.tools.MiniResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
//@DataSource(DataSourceType.SLAVE)
@Slf4j
public class NeedServiceImpl implements INeedService {

    @Autowired
    private NeedMapper needMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    NeedMsgMapper needMsgMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
//    @DataSource(DataSourceType.SLAVE)
    public Need selectNeedById(Long id)
    {
        return needMapper.selectNeedById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param need 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
//    @DataSource(DataSourceType.SLAVE)
    public List<Need> selectNeedList(Need need)
    {
        return needMapper.selectNeedList(need);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param need 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertNeed(Need need)
    {
        return needMapper.insertNeed(need);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param need 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateNeed(Need need)
    {
        return needMapper.updateNeed(need);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteNeedByIds(Long[] ids)
    {
        return needMapper.deleteNeedByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteNeedById(Long id)
    {
        return needMapper.deleteNeedById(id);
    }

    @Override
//    @DataSource(DataSourceType.SLAVE)
    public List<Map> getList(Integer status) {
        return needMapper.selectAllNeeds(status);
    }



    @Override
    public Map getInfo(Long id,Long uid) {
        Map info = this.needMapper.getInfo(id);
        if (!ObjectUtils.isEmpty(info)) {
            Need need = needMapper.selectNeedById(id);
            if (need.getAnonymous() == 2) {
                User user = userMapper.selectUserById(need.getUserId());
                if (!ObjectUtils.isEmpty(user)) {
                    info.put("username",user.getNickname());
                }else{
                    info.put("username","匿名");
                }
            }else{
                info.put("username","匿名");
            }

            if (!ObjectUtils.isEmpty(uid) && uid == need.getUserId()) {
                List<Map> needMagList = needMsgMapper.getNeedMagList(id, 1);
                info.put("msgList",needMagList);
            }

            info.put("tel", "如需沟通联系客服:<br/> <a href=\"tel:010-85315368\">010-85315368</a> | <a href=\"tel:021-52996138\">021-52996138</a> | <a href=\"tel:18612916908\">18612916908</a>");
            need.setViews(ObjectUtils.isEmpty(need.getViews()) ? 0L : need.getViews() + 1L);
            needMapper.updateNeed(need);
        }
        return info;
    }


    @Override
//    @DataSource(DataSourceType.SLAVE)
    public Object postSave(String token, NeedDto needDto) {
        Long userId = JWTUtils.getUserid(token);
        if (ObjectUtils.isEmpty(userId)) {
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }else{
            Need need = needMapper.selectNeedById(needDto.getId());
            Boolean isInsert = false;
            if (ObjectUtils.isEmpty(need)) {
                need = new Need();
                Long maxId = needMapper.getMaxId();
                need.setId(maxId + 1);
                need.setStatus(0);
                need.setViews(1L);
                need.setSort(0L);
                isInsert = true;
            }

            need.setUserId(userId);
            need.setTitle(needDto.getTitle());
            need.setDesc(needDto.getDesc());
            need.setContent(needDto.getContent());
            need.setDays(needDto.getDays());
            need.setAnonymous(needDto.getAnonymous());
            need.setPhone(needDto.getPhone());
            Date date = DateUtil.addDays(Integer.valueOf(needDto.getDays()));
            need.setEndTime(DateUtil.format(date));

//            log.info("插入数据！》》》》》》》》》》》》》");
            if (isInsert) {
                needMapper.insertNeed(need);
            }else{
                need.setViews(need.getViews() + 1);
                needMapper.updateNeed(need);
            }
            return new MiniResult(200,"保存成功,等待管理审核...",null);
        }
    }

    @Override
//    @DataSource(DataSourceType.SLAVE)
    public Object postSaveMsg(String token, NMsgDto nMsgDto) {
        Long userId = JWTUtils.getUserid(token);
        if (ObjectUtils.isEmpty(userId)) {
            Map reMap = new HashMap();
            reMap.put("code",401);
            reMap.put("msg","Token过期,或无此用户");
            return reMap;
        }else{
            Long maxid = needMsgMapper.getMaxid();
            NeedMsg nm = new NeedMsg();
            nm.setId(maxid + 1);
            nm.setContent(nMsgDto.getContent());
            nm.setNid(nMsgDto.getId());
            nm.setUid(nMsgDto.getUid());
            needMsgMapper.insertNeedMsg(nm);
            return new MiniResult(200,"留言成功",null);
        }

    }

    @Override
    public List<Map> getNeedsByUserId(Long userId,Integer status) {

        return this.needMapper.getNeedsByUserId(userId,status);
    }

    @Override
    public Map getNeedInfo(Long id) {
        Map info = needMapper.getInfo(id);
        return info;
    }

    /**
     * 根据需求id 获得留言信息
     *
     * @param nid
     * @return
     */
    @Override
    public List<NeedMsgVo> getNeedMsgList(Long nid) {
        NeedMsg needMsg = new NeedMsg();
        needMsg.setNid(nid);
        List<NeedMsg> needMsgs = needMsgMapper.selectNeedMsgList(needMsg);
        List<NeedMsgVo> needMsgVos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(needMsgs)) {
            for (NeedMsg msg : needMsgs) {
                NeedMsgVo needMsgVo = new NeedMsgVo();
                BeanUtils.copyProperties(msg, needMsgVo);
                User user = userMapper.selectUserById(msg.getUid());
                needMsgVo.setUser(user);
                needMsgVos.add(needMsgVo);
            }
        }
        return needMsgVos;
    }
}
